<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">商家提现申请表</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home">首页</a>
          <a><cite>商家提现申请表</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
                      搜索：  <select id="businesscashapply-search-key">
            <option value="">-请选择-</option>
                 <!--<option value="id">ID</option>-->
                 <option value="business_id">商家ID</option>
                 <!--<option value="check_user_id">审核人ID</option>-->
                 <!--<option value="cash_user_id">转账人ID</option>-->
                 <!--<option value="apply_user_id">申请人ID</option>-->
                 <!--<option value="cash_amount">转账金额</option>-->
                 <!--<option value="charge_fee">手续费</option>-->
                 <!--<option value="actual_amount">实际到账</option>-->
                 <!--<option value="status">状态，1-待审核，2-已审核，3-已转帐， -1-驳回</option>-->
                 <!--<option value="cash_style">提现方式，1-线下，2-到微信钱包，3-到银行卡</option>-->
                 <!--<option value="description">说明</option>-->
                 <!--<option value="create_date">创建时间</option>-->
                 <!--<option value="check_date">审核时间</option>-->
                 <!--<option value="cash_date">转账时间</option>-->
                 </select>&emsp;
            <input id="businesscashapply-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="businesscashapply-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="businesscashapply-table" lay-filter="businesscashapply-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="businesscashapply-table-bar">
 {{#   if(admin.hasPerm("business:businesscashapply:update") ){   }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
 {{#     } }} 
 {{#  if(admin.hasPerm("business:businesscashapply:delete") ){   }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
 {{#     } }} 
</script>


<script type="text/html" id="toolbar">
     {{#  if(admin.hasPerm("business:businesscashapply:save")){ }}
     <button id="businesscashapply-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
     {{#     } }} 
 
</script>


<script type="text/html" id="newApply">
    <div class="layui-form-item">
        <label class="layui-form-label">商家名称:</label>
        <div class="layui-input-inline">
            <label class="layui-form-label">{{d.businessName}}</label>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">佣金余额:</label>
        <div class="layui-input-inline">
            <label class="layui-form-label">{{d.commissionBalance}}</label>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">提现金额:</label>
        <div class="layui-input-block">
            <input name="cashAmount" placeholder="0" type="text" style="display: inline;width: 100px" class="layui-input" maxlength="20"  id="cashAmount"
                  lay-verify="required" required/><button type="button" style="margin-left:20px;display: inline" id="btngetall" class="layui-btn layui-btn-normal">提取全部</button>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手续费:</label>
        <div class="layui-input-block">
            <label class="layui-form-label" id="chargeFee">¥ 0</label>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">实收金额:</label>
        <div class="layui-input-inline">
            <label class="layui-form-label" id="actualAmount">¥ 0</label>
        </div>
    </div>
</script>


<script type="text/html" id="btstatus">
    {{# if(d.status == 1){ }}
    待审核
    {{# } }}

    {{# if(d.status == 2){ }}
    已审核
    {{# } }}
    {{# if(d.status == 3){ }}
    已转账
    {{# } }}
    {{# if(d.status == -1){ }}
    驳回
    {{# } }}
</script>

<script type="text/html" id="btgetstyle">
    {{# if(d.cashStyle == 1){ }}
    线下支付
    {{# } }}

    {{# if(d.cashStyle == 2){ }}
    微信钱包
    {{# } }}
    {{# if(d.cashStyle == 3){ }}
    银行卡
    {{# } }}
</script>

<script>
   var admin;
    layui.use(['form', 'table', 'util', 'config', 'admin', 'upload','laytpl','element' ], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
          admin = layui.admin;
        //上传
        var upload = layui.upload;
        let laytpl = layui.laytpl;
        let element = layui.element;

        // 渲染表格
        table.render({
            elem: '#businesscashapply-table',
            url: config.base_server + 'business/businesscashapply/list',
            method: 'GET',
       
            page: true,
            cols: [[
                {type: 'numbers'},
                           // {field: 'id', sort: true, title: 'ID'},
                           {field: 'businessName', sort: false, title: '商家ID'},
                           {field: 'checkUsername', sort: false, title: '审核人'},
                           {field: 'cashUsername', sort: false, title: '转账人'},
                           {field: 'applyUsername', sort: false, title: '申请人'},
                           {field: 'cashAmount', sort: false, title: '转账金额'},
                           {field: 'chargeFee', sort: false, title: '手续费'},
                           {field: 'actualAmount', sort: false, title: '实际到账'},
                           {field: 'status', sort: false, title: '提现状态',toolbar:'#btstatus'},
                           {field: 'cashStyle', sort: false, title: '提现方式',toolbar:'#btgetstyle'},
                           {field: 'description', sort: false, title: '说明'},
                           {field: 'createDate', sort: false, title: '创建时间'},
                           {field: 'checkDate', sort: false, title: '审核时间'},
                           {field: 'cashDate', sort: false, title: '转账时间'},
                        
                {fixed: 'right', align: 'center', toolbar: '#businesscashapply-table-bar', title: '操作', width: 175}
            ]]
        });
        
        laytpl($("#toolbar")[0].innerHTML).render({ }, function(html){
			  $(".layui-card-body .toolbar").append(html);
			  element.render('nav');
		  });


        // 添加按钮点击事件
        $('#businesscashapply-btn-add').click(function () {
         //  showEditModel();
            admin.req('business/businesswallet/currinfo', {}, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    //打开窗口
                    console.log(data);
                    var businessName = data.data.businessName;
                    var commissionBalance = '¥ '+data.data.commissionBalance;
                    var pickCashFee = data.data.pickCashFee;
                    laytpl($("#newApply")[0].innerHTML).render({businessName:businessName,commissionBalance:commissionBalance}, function(html){
                        admin.popupCenter({
                            title: "商家提现申请",
                            area: ['620px', '500px'],
                            content:html,
                            btn: ['提交申请', '取消申请'],
                            yes: function(){
                                // $(that).click();
                                var cashAmount = $("#cashAmount").val();
                                admin.req('business/businesscashapply/newapply',JSON.stringify({cashAmount:cashAmount}),function (res) {
                                    // console.log(res);

                                    layer.msg(res.msg, {icon: 2, time: 500});
                                    admin.closePopupCenter();
                                    table.reload('businesscashapply-table', {});
                                },'POST')
                            },
                            btn2: function(){
                                layer.closeAll();
                            },

                            finish: function () {
                                table.reload('businesscashapply-table', {});
                            }
                        });
                    });


                    //获取全部佣金余额
                    $('#btngetall').click(function () {
                        $('#cashAmount').val(data.data.commissionBalance);

                        var pick = parseFloat($("#cashAmount").val()).toFixed(2);
                        var feerate = parseFloat(pickCashFee).toFixed(2);
                        var fee = parseFloat(pick*feerate).toFixed(2);
                        var actual = parseFloat(pick - fee).toFixed(2);
                        $('#chargeFee').text('¥ '+fee?fee:0);
                        $('#actualAmount').text('¥ '+actual?actual:0);
                    });
                    //计算手续费
                    $("#cashAmount").change(function(){
                        // console.log($("#cashAmount").val()+"---"+pickCashFee);
                        var pick = parseFloat($("#cashAmount").val()).toFixed(2);
                        var feerate = parseFloat(pickCashFee).toFixed(2);
                        var fee = parseFloat(pick*feerate).toFixed(2);
                        var actual = parseFloat(pick - fee).toFixed(2);
                        $('#chargeFee').text('¥ '+fee?fee:0);
                        $('#actualAmount').text('¥ '+actual?actual:0);

                    });

                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, 'GET');



        });

        // 工具条点击事件
        table.on('tool(businesscashapply-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
             
                showEditModel(data);
            }  else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('business/businesscashapply/delete/'+obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('businesscashapply-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'delete');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
        	  var title = data ? '修改商家提现' : '添加商家提现';
            //修改方法名
            if (data){
                data.method="update"
            }else{
            	data={method:"save"};
            }
 
            admin.putTempData('t_businesscashapply', data);
            admin.popupCenter({
                title: title,
                path: 'pages/business/businesscashapply_form.html',
                finish: function () {
                    table.reload('businesscashapply-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#businesscashapply-btn-search').click(function () {
            var key = $('#businesscashapply-search-key').val();
            var value = $('#businesscashapply-search-value').val();
            table.reload('businesscashapply-table', {where: {searchKey: key, searchValue: value}});
        });

    });
</script>
